﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using SilverlightDebateView;
using SilverFlow.Controls;
using System.Linq;
using SilverlightDebateViewModel;

namespace SilverlightDebateView.Behaviors
{
    public class OpenStatementsBehavior : TargetedTriggerAction<FloatingWindowHost>
    {
        FloatingWindowHost objFloatingWindowHost;
        private Point startPoint = new Point(50, 50);

        protected override void OnAttached()
        {
            base.OnAttached();
            objFloatingWindowHost = (FloatingWindowHost)(this.AssociatedObject);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
        }

        protected override void Invoke(object parameter)
        {
            // Only add if not already added

            var result = (from fWindow in objFloatingWindowHost.Children.Cast<FrameworkElement>()
                          where fWindow.Tag as string == "Search"
                          select fWindow).FirstOrDefault();

            if (result == null)
            {
                Statements StatementsWindow = new Statements();

                // Get DataContext of the Host
                MainPageViewModel objMainPageViewModel = (MainPageViewModel)objFloatingWindowHost.DataContext;
                // Set the DataContext for Statements
                StatementsWindow.DataContext = objMainPageViewModel.VMStatementsViewModel;
                // Add The Statements 
                objFloatingWindowHost.Add(StatementsWindow);
                string title = "Search";
                StatementsWindow.Tag = "Search";
                StatementsWindow.Title = title;
                StatementsWindow.IconText = title;
                StatementsWindow.Show(startPoint);
            }
        }
    }
}
